/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import Handlers.TaskHandler;
import ObjectClass.Task;
import java.util.ArrayList;

/**
 *
 * @author Jeppe
 */
public class SøgOpgaver extends javax.swing.JPanel {

    private Main main;
    private TaskHandler taskHandler;
    private String searchType;

    /**
     * Creates new form SøgOpgaver
     */
    public SøgOpgaver(Main main) {
        initComponents();
        this.main = main;
        this.taskHandler = main.getTaskHandler();
        searchType = "Fejl";
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSeparator1 = new javax.swing.JSeparator();
        jPanel1 = new javax.swing.JPanel();
        SeÆndreOrdre_SøgDatoKnap = new javax.swing.JButton();
        comboÅr = new javax.swing.JComboBox();
        comboMåned = new javax.swing.JComboBox();
        ComboDag = new javax.swing.JComboBox();
        tlfnrtxt = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        taskList = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        TaskInfoArea = new javax.swing.JTextArea();
        tilbageKnap = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(1070, 632));
        setPreferredSize(new java.awt.Dimension(1070, 632));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, -30, 10, 730));

        jPanel1.setOpaque(false);

        SeÆndreOrdre_SøgDatoKnap.setText("Søg");
        SeÆndreOrdre_SøgDatoKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SeÆndreOrdre_SøgDatoKnapActionPerformed(evt);
            }
        });

        comboÅr.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023", "2024", "2025", "2026", "2027", "2028", "2029", "2030", "2031", "2032", "2033", "2034", "2035", "2036", "2037", "2038", "2039", "2040", "2041", "2042", "2043", "2044", "2045", "2046", "2047", "2048", "2049", "2050", "2051", "2052", "2053", "2054", "2055", "2056", "2057", "2058", "2059", "2060", "2061", "2062", "2063", "2064", "2065", "2066", "2067", "2068", "2069", "2070", "2071", "2072", "2073", "2074", "2075", "2076", "2077", "2078", "2079", "2080", "2081", "2082", "2083", "2084", "2085", "2086", "2087", "2088", "2089", "2090", "2091", "2092", "2093", "2094", "2095", "2096", "2097", "2098", "2099", "2100" }));
        comboÅr.setSelectedItem(2012);

        comboMåned.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Januar", "Februar", "Marts", "April", "Maj", "Juni", "Juli", "August", "September", "Oktober", "November", "December" }));

        ComboDag.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));

        tlfnrtxt.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tlfnrtxtFocusLost(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Tlfnr:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("Dato:");

        taskList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                taskListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(taskList);

        TaskInfoArea.setColumns(20);
        TaskInfoArea.setRows(5);
        jScrollPane2.setViewportView(TaskInfoArea);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(11, 11, 11)
                        .addComponent(tlfnrtxt, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(37, 37, 37)
                                .addComponent(ComboDag, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(comboMåned, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(comboÅr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(SeÆndreOrdre_SøgDatoKnap, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 330, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(100, 100, 100)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 413, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(7, 7, 7)
                                .addComponent(jLabel1))
                            .addComponent(tlfnrtxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(12, 12, 12)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(jLabel5))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(ComboDag, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(comboMåned, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(comboÅr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(SeÆndreOrdre_SøgDatoKnap)))
                        .addGap(16, 16, 16)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 346, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 60, 880, 560));

        tilbageKnap.setText("Tilbage");
        tilbageKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tilbageKnapActionPerformed(evt);
            }
        });
        add(tilbageKnap, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, -1, -1));

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Pics/Baggrund 2.PNG"))); // NOI18N
        add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 1020, 700));
    }// </editor-fold>//GEN-END:initComponents

    private void SeÆndreOrdre_SøgDatoKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SeÆndreOrdre_SøgDatoKnapActionPerformed
        taskList.setListData(taskHandler.getTaskListByDato(ComboDag.getSelectedIndex() + 1, comboMåned.getSelectedIndex() + 1, Integer.parseInt(comboÅr.getSelectedItem().toString())).toArray());
        if (taskHandler.getTaskListByDato(ComboDag.getSelectedIndex() + 1, comboMåned.getSelectedIndex() + 1, Integer.parseInt(comboÅr.getSelectedItem().toString())).size() >= 1) {
            searchType = "Dato";
        }
        System.out.println("DATO: " + (ComboDag.getSelectedIndex()+1) + "." + (comboMåned.getSelectedIndex()+1) + "-" + Integer.parseInt(comboÅr.getSelectedItem().toString()));
    }//GEN-LAST:event_SeÆndreOrdre_SøgDatoKnapActionPerformed

    private void tlfnrtxtFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tlfnrtxtFocusLost
        if (tlfnrtxt.getText().matches("\\d+")) {
            ArrayList<Task> demandTaskList = taskHandler.getTaskListByPhoneNo(Integer.parseInt(tlfnrtxt.getText()));
            taskList.setListData(demandTaskList.toArray());    
            searchType = "tlfNr";
        } else if(tlfnrtxt.getText().equals("")){
            ArrayList<Task> demandTaskList = taskHandler.getTaskList();
            taskList.setListData(demandTaskList.toArray());
            searchType = "ALLE";
        }else{
            taskList.removeAll();
            System.out.println("Du har ikke skrevet et valid tlf nr");
        }
        
    }//GEN-LAST:event_tlfnrtxtFocusLost

    private void tilbageKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tilbageKnapActionPerformed
        main.visStartPanel();
    }//GEN-LAST:event_tilbageKnapActionPerformed

    private void taskListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_taskListValueChanged
        if (taskList.getSelectedIndex() >= 0) {
            Task task= null;
            if (searchType.equals("tlfNr")) {
                task = taskHandler.getTaskListByPhoneNo(Integer.parseInt(tlfnrtxt.getText())).get(taskList.getSelectedIndex());
            } else if (searchType.equals("Dato")) {
                task = taskHandler.getTaskListByDato(ComboDag.getSelectedIndex() + 1, comboMåned.getSelectedIndex() + 1, Integer.parseInt(comboÅr.getSelectedItem().toString())).get(taskList.getSelectedIndex());
            }else if (searchType.equals("ALLE")){
                task = taskHandler.getTaskList().get(taskList.getSelectedIndex());
            }
            TaskInfoArea.setText(task.toStringInfo());
        }
    }//GEN-LAST:event_taskListValueChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox ComboDag;
    private javax.swing.JButton SeÆndreOrdre_SøgDatoKnap;
    private javax.swing.JTextArea TaskInfoArea;
    private javax.swing.JComboBox comboMåned;
    private javax.swing.JComboBox comboÅr;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JList taskList;
    private javax.swing.JButton tilbageKnap;
    private javax.swing.JTextField tlfnrtxt;
    // End of variables declaration//GEN-END:variables
}
